﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{    
    public partial class JLTRPAVData : BaseFunction
    {
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        /*public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }

        SETINTEXDB INTEXDB = new SETINTEXDB();
         */
        public JLTRPAVData()
            : base()
        {
        }
         public JLTRPAVData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }

        public DataSet GetRPAV(DataRow dwTABEL)
        {
            DataSet Result;
            DataSet Return = new DataSet();
            string TableName = "RPAV";
            string CO = dwTABEL["CO"].ToString().Trim();
            string MACHINE = dwTABEL["MACHINE"].ToString().Trim();
            string FROMDATE = dwTABEL["FROMDATE"].ToString().Trim();
            string ENDDATE = dwTABEL["ENDDATE"].ToString().Trim();
            string FROMDAT = dwTABEL["FROMDAT"].ToString().Trim();
            string ENDDAT = dwTABEL["ENDDAT"].ToString().Trim();
            try
            {
                orlReadCommand = INTEXDBC.OracleDB.CreateCommand();
                orlReadCommand.CommandText = " SELECT TRIM(A.ABPART)||'-'||TRIM(A.ABPFARBE) AS ARTICLE, "
                    + " TO_NUMBER(ABFIMLFDNR) AS MACHINE,ABFISTARTDAT AS STARTDAT, "
                    + " SUBSTR(LPAD(ABFISTARTZT,4,0),1,2)||':'||SUBSTR(LPAD(ABFISTARTZT,4,0),3) AS STARTTIME, "
                    + " ABFIENDDAT AS ENDDAT, "
                    + " SUBSTR(LPAD(ABFIENDZT,4,0),1,2)||':'||SUBSTR(LPAD(ABFIENDZT,4,0),3) AS ENDTIME, "
                    + " A.ABPZUGET*0.01 AS QTY,C.BZBAUMKRS||'-'||TO_NUMBER(C.BZBAUMLFD) AS BEAM, "
                    + " A.ABPAUFKRS||'-'||TO_NUMBER(A.ABPAUFLFD)||'-'||TO_NUMBER(A.ABPAUFPOS) AS ORD "
                    + " FROM ABPOS A,ABFOLGE B,BAZU C "
                    + " WHERE A.ABPMANDANT = B.ABFMANDANT AND A.ABPAUFKRS = B.ABFAUFKRS "
                    + " AND A.ABPAUFLFD = B.ABFAUFLFD AND A.ABPAUFPOS = B.ABFAUFPOS "
                    + " AND A.ABPMANDANT = C.BZMANDANT(+) AND A.ABPAUFKRS = C.BZAUFKRS(+) "
                    + " AND A.ABPAUFLFD = C.BZAUFLFD(+) AND A.ABPAUFPOS = C.BZAUFPOS(+) "
                    + " AND B.ABFMTYP = 'ZJ      ' AND B.ABFISTARTDAT >0 "
                    + " AND (TO_NUMBER(ABFIMLFDNR) = '" + MACHINE + "' OR '" + MACHINE + "' = 'AAAA') "
                    + " AND TRIM(A.ABPART)||'-'||TRIM(A.ABPFARBE) LIKE '"+CO+"%' "
                    + " AND ABFISTARTDAT >= '" + FROMDATE + "' AND ABFISTARTDAT <= '" + ENDDATE + "' "
                    + " AND ABFIENDDAT >= '" + FROMDAT + "' AND ABFIENDDAT <= '" + ENDDAT + "' "
                    + " ORDER BY STARTDAT "
                    ;

                Result = INTEXDBC.Connect2Proxy(orlReadCommand);
                if (Result.Tables.Count > 0)
                {
                    Result.Tables[0].TableName = TableName;
                    //byte[] b;
                    //string value;
                    //foreach (DataRow dw in Result.Tables[TableName].Rows)
                    //{
                    //    b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw["REMARK"].ToString().Trim());
                    //    value = Encoding.GetEncoding("gb2312").GetString(b);
                    //    dw["REMARK"] = value;
                       
                    //}
                }
                Return.Merge(Result);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return Return;
        }
        
        public DataSet BuildData(DataRow dwUI)
        {
            /*DataSet SResult = new DataSet();
            SETINTEXDB SReturn = new SETINTEXDB(); */
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                setResult = GetRPAV(dwUI);
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }

    }
}